<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form onsubmit="return false">
        <p><label for="">用户名:</label><input type="text" class="user"><span></span></p>
        <p><label for="">密&emsp;码:</label><input type="text" class="pwd"><span></span></p>
        <p><button class="btn1">登录</button><button class="btn2">重置</button></p>
    </form>
    <script src="../js/cookie.js"></script>
    <script>
        const user = document.querySelector(".user");
        const pwd = document.querySelector(".pwd");
        const btn1 = document.querySelector(".btn1");
        const btn2 = document.querySelector(".btn2");
        btn1.onclick = function () {
            if (user.value && pwd.value) {
                var xhr = new XMLHttpRequest();
                xhr.open("get", "../php/sgin.php?user=" + user.value + "&pwd=" + pwd.value, true);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        var str = JSON.parse(xhr.responseText);
                        if (str.status == true) {
                            if (getCookie("lgc")) {
                                delCookie("lgc");
                                setCookie("lgc", user.value, 7);
                                alert(str.detail + "点击确认后进入首页");
                                location.href = "./index.html";
                            } else {
                                setCookie("lgc", user.value, 7);
                                alert(str.detail + "点击确认后进入首页");
                                location.href = "./index.html";
                            }

                        } else {
                            alert(str.detail + "点击确认后重新输入用户名和密码");
                            location.reload();
                        }
                    }
                }
            }
        }
    </script>
</body>

</html>